<template>


  <div>

    <!--  查询-->
    <el-card>

      <el-form inline label-width="90px" size="mini" class="demo-form-inline">

        <el-form-item label="应用名称">
          <el-input placeholder="请输入应用名称" v-model.trim="queryParam.name" clearable size="mini"></el-input>
        </el-form-item>
        <el-form-item label="公共项目">
          <el-select v-model="queryParam.common" placeholder="请选择" clearable  >
            <el-option key="1" label="公共项目" :value="1"/>
            <el-option key="0" label="非公共项目" :value="0"/>
          </el-select>
        </el-form-item>
        <el-form-item label="应用状态">
          <el-select v-model="queryParam.state" placeholder="请选择" clearable  >
            <el-option key="1" label="立项" :value="1"/>
            <el-option key="2" label="开发" :value="2"/>
            <el-option key="4" label="维护" :value="4"/>
            <el-option key="99" label="废弃" :value="99"/>
          </el-select>
        </el-form-item>
        <el-form-item label="项目类型">
          <el-select v-model="queryParam.type" placeholder="请选择" clearable  >
            <el-option key="1" label="app" :value="1"/>
            <el-option key="2" label="前端" :value="2"/>
            <el-option key="3" label="后端" :value="3"/>
            <el-option key="4" label="小程序" :value="4"/>
          </el-select>
        </el-form-item>
        <el-form-item label="开发语言">
          <el-input placeholder="请输入开发语言" v-model.trim="queryParam.language" clearable size="mini"></el-input>
        </el-form-item>
        <el-form-item label="重要等级">
          <el-select v-model="queryParam.importance" placeholder="请选择" clearable  >
            <el-option key="1" label="辅助应用" :value="1"/>
            <el-option key="2" label="普通应用" :value="2"/>
            <el-option key="3" label="重点应用" :value="3"/>
            <el-option key="4" label="核心应用" :value="4"/>
          </el-select>
        </el-form-item>
        <el-form-item label="微服务系统">
          <el-select v-model="queryParam.microservices" placeholder="请选择" clearable  >
            <el-option key="1" label="微服务应用" :value="1"/>
            <el-option key="0" label="非微服务应用" :value="0"/>
          </el-select>
        </el-form-item>
        <el-form-item label="分布式系统">
          <el-select v-model="queryParam.distributed" placeholder="请选择" clearable  >
            <el-option key="1" label="分布式系统" :value="1"/>
            <el-option key="0" label="非分布式系统" :value="0"/>
          </el-select>
        </el-form-item>
        <el-form-item label="负责人姓名">
          <el-input placeholder="请输入应用负责人姓名" v-model.trim="queryParam.directorName" clearable size="mini"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="success" size="mini" @click="getListPage">查询</el-button>
          <el-button type="info" size="mini" @click="resetQuery">重置</el-button>
          <el-button type="warning" size="mini" @click="openSaveBefore">新增</el-button>
        </el-form-item>

      </el-form>

    </el-card>




    <!--列表-->
    <el-card style="margin-top: 10px; min-height: 700px">

      <div class="container">

        <!--一个小框框-->
        <div class="box" v-for="(item, index) in tableData" :key="index">
          <div class="version-tag2 component-title" v-if="item.common === 1">公共应用</div>
          <div class="version-tag component-title" v-if="item.common === 0">非公共应用</div>
          <div class="box-content" >

            <el-row :gutter="10">
              <el-col :span="9">
                <div class="block">
                  <el-image class="component-ico" fit="contain" :src="item.icoUrl || getDefaultIcon(item.type)" />
                </div>
              </el-col>
              <el-col :span="15">

                <el-row :gutter="10">
                  <el-col :span="24">
                    <h3 class="component-title"  @click="openAppLayout(item)">{{item.name}}</h3>
                  </el-col>
                </el-row>

                <!--<el-row>-->
                <!--  <el-col :span="8">支持环境：</el-col>-->
                <!--  <el-col :span="16">-->
                <!--    <span>{{item.osStr}}</span>-->
                <!--  </el-col>-->
                <!--</el-row>-->

                <el-row >
                  <el-col :span="8">状态：</el-col>
                  <el-col :span="16">
                    <el-tag size="mini" type="warning" v-if="item.state === 1">立项</el-tag>
                    <el-tag size="mini" v-else-if="item.state === 2">开发</el-tag>
                    <el-tag size="mini" type="success" v-else-if="item.state === 4">维护</el-tag>
                    <el-tag size="mini" type="info" v-else-if="item.state === 99">废弃</el-tag>
                  </el-col>
                </el-row>

                <el-row style="margin-top: 5px">
                  <el-col :span="8">类型：</el-col>
                  <el-col :span="16">
                    <el-tag size="mini" type="success" v-if="item.type === 1">app</el-tag>
                    <el-tag size="mini" type="warning" v-else-if="item.type === 2">前端</el-tag>
                    <el-tag size="mini" v-else-if="item.type === 3">后端</el-tag>
                    <el-tag size="mini" type="danger" v-else-if="item.type === 4">小程序</el-tag>
                  </el-col>
                </el-row>

                <el-row style="margin-top: 5px">
                  <el-col :span="8">安装数量：</el-col>
                  <el-col :span="16">
                    <el-tag size="mini" type="warning">{{item.installNum}}</el-tag>
                  </el-col>
                </el-row>


              </el-col>
            </el-row>

            <p style="margin-top: 0px; margin-bottom: 0px;">详情：</p>
            <p class="description" @click="openAppLayout(item)" >{{item.briefIntroduction}}</p>


            <!-- 固定在右下角的按钮组 -->
            <div class="button-group">
              <el-button type="primary" size="mini" @click="openUpdBefore(item)"  >编辑</el-button>
              <el-button type="danger" size="mini" @click="delTips(item)"  >删除</el-button>
            </div>

          </div>
        </div>
      </div>

      <!--分页部分-->
      <el-pagination
        style="width:33%; margin-right:10px; margin-top:10px;"
        background
        layout="total, prev, pager, next, jumper"
        @current-change="pageCurrentChange"
        :page-size="pageConfig.size"
        :current-page="pageConfig.current"
        :pager-count="5"
        :total="pageConfig.total">
      </el-pagination>

    </el-card>




    <AppSaveUpd  ref="OpsAppSaveUpdRef" @getListPage="getListPage"/>

  </div>

</template>


<script>

import http from '@/utils/http'
import AppSaveUpd  from './AppSaveUpd.vue'


export default {
  name: "OpsAppList",
  components: {
    AppSaveUpd,
  },
  data() {
    return {
      // 查询参数
      queryParam: {
        id: null,
        name: null,
        common: null,
        state: null,
        type: null,
        language: null,
        os: null,
        importance: null,
        microservices: null,
        distributed: null,
        packMethodDoc: null,
        deployDoc: null,
        tableUrl: null,
        git: null,
        briefIntroduction: null,
        installDoc: null,
        sort: null,
        openTime: null,
        directorName: null,
        createTime: null,
        updateTime: null,
        updateId: null,
        createId: null,
        delType: null,
      },
      // 分页参数
      pageConfig: {
        // 每页记录数
        size: 8,
        // 当前页码
        current: 1,
        // 总记录数
        total: 0,
      },
      // 表格数据
      tableData: [],
    }
  },
  methods: {
    /**
     * 渲染图片
     * 只能这样做，图片这里不支持非固定地址返回
     * @param item
     */
    getImageUrlInfo(item) {
      if (!item || !item.ico || item.ico === 'null') {
        // 判空或无效，直接显示默认图
        this.$set(item, 'icoUrl', null);
        return;
      }

      http.getImageUrl(item.ico)
        .then(url => {
          // 异步赋值，触发响应式更新
          this.$set(item, 'icoUrl', url);
        })
        .catch(() => {
          // 下载失败 fallback 默认图
          this.$set(item, 'icoUrl', null);
        });
    },
    /**
     * 获取默认的图标
     * @param type
     * @returns {*}
     */
    getDefaultIcon(type) {
      switch(type) {
        case 1:
          return require('@/assets/images/ico/app3.png');
        case 2:
          return require('@/assets/images/ico/web.png');
        case 3:
          return require('@/assets/images/ico/java.png');
        case 4:
          return require('@/assets/images/ico/smallProgram.png');
        default:
          return require('@/assets/images/ico/app2.png');
      }
    },
    /**
     * 打开组件详情
     * @param obj 组件信息
     */
    openAppLayout(obj){
      this.$router.push({path: '/toAppLayout', query: {appId: obj.id}})
    },
    /**
     * 打开保存的弹框
     */
    openSaveBefore() {
      this.$refs.OpsAppSaveUpdRef.init('save', null)
    },
    /**
     * 查看之前触发
     * @param obj 这一行数据
     */
    openViewBefore(obj){
      this.$refs.OpsAppDetailedRef.init(obj)
    },
    /**
     * 打开修改之前的的弹出框
     * @param obj 修改的值
     */
    openUpdBefore(obj) {
      this.$refs.OpsAppSaveUpdRef.init('upd', obj)
    },
    /**
     * 删除之前的提示框
     * @param obj 要删除的数据
     */
    delTips(obj) {
      this.$confirm('是否执行删除, 删除后无法恢复，是否继续?', '提示', {
        confirmButtonText: '确定删除',
        cancelButtonText: '取消删除',
        type: 'warning'
      }).then(() => {
        // 确定删除
        this.delOpsApp(obj)

      }).catch(() => {
        // 取消删除
      });
    },
    /**
     * 删除
     * @param obj 要删除的数据
     */
    delOpsApp(obj) {
      http.post("opsApp/delOpsApp", {
        opsAppId: obj.id,
      }).then(response => {
        let res = response.data;
        this.getListPage()
        this.$message.success("删除成功");

      }).catch(error => {
        this.$message.error (error.message)
      })
    },
    /**
     * 分支分页查询列表
     */
    getListPage() {
      this.queryParam.current = this.pageConfig.current
      this.queryParam.size = this.pageConfig.size
      http.post("/opsApp/getOpsAppPageList", this.queryParam).then(response => {
        let res = response.data;
        // 分页数据
        let data = res.data;
        this.pageConfig.total = data.total;
        this.pageConfig.current = data.current;
        this.pageConfig.size = data.size;
        // 表格数据
        this.tableData = data.records
        // 异步加载每个图片
        this.tableData.forEach(item => this.getImageUrlInfo(item));

      }).catch(error => {
        this.$message.error (error.message);
      })
    },
    /**
     * 分页被改变的时候
     * @param value 当前页
     */
    pageCurrentChange(value) {
      this.pageConfig.current = value
      this.getListPage()
    },
    /**
     * 重置查询条件
     */
    resetQuery() {
      this.queryParam = {
        id: null,
        name: null,
        common: null,
        state: null,
        type: null,
        language: null,
        os: null,
        importance: null,
        microservices: null,
        distributed: null,
        packMethodDoc: null,
        deployDoc: null,
        tableUrl: null,
        git: null,
        briefIntroduction: null,
        installDoc: null,
        sort: null,
        openTime: null,
        directorName: null,
      }
    },
    /**
     * 分页前重置分页参数
     */
    getPageListBefore() {
      this.pageConfig.current = 1
      this.getListPage()
    },
  },
  // 进入马上执行
  mounted() {
    this.getListPage()
    this.$store.commit ('navigationListChange', [
      {name: '应用管理', url: '/toAppList', query: {}},
    ])
  },
}
</script>

<style scoped>


/**
====== 一个方框 start ===
 */

.container {
  display: flex;
  flex-wrap: wrap; /* 允许换行 */
  gap: 20px; /* 小框框之间的间距 */
  padding: 20px;
  min-height: 600px;
}

.component-ico {
  width: 126px;
  height: 110px;
}

.box {
  width: 380px; /* 固定宽度 */
  height: 270px; /* 固定高度 */
  min-height: 270px;
  background-color: white;
  border: 1px solid #ddd;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  /*overflow: hidden;*/
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  position: relative; /* 添加这个以便version-tag可以绝对定位 */
  overflow: hidden; /* 保持hidden防止内容溢出 */
}

.box-content {
  flex: 1;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.box-content .description {
  margin-top: 0px;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2; /* 显示3行 */
  -webkit-box-orient: vertical;
  cursor: pointer;
  margin-bottom: 6px;
}

.component-title {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 1;
  overflow: hidden;
  cursor: pointer;
}

/* 角标 start */

.version-tag {
  /*--t: 10px; !* 上方偏移距离 *!*/
  position: absolute;
  padding: 0 10px;
  height: 20px;
  line-height: 20px;
  font-weight: bold;
  inset: 0 0 auto auto; /* top 和 right 的值 0 */
  transform-origin: 0 0; /* 或 top left */
  transform: translate(29.3%) rotate(45deg);
  background: #D4E6F1;
  /*box-shadow: 0 0 0 999px #8FBCDA;*/
  box-shadow: 0 0 0 999px #D4E6F1;
  clip-path: inset(0 -100%);
}

.version-tag2 {
  /*--t: 10px; !* 上方偏移距离 *!*/
  position: absolute;
  padding: 0 10px;
  height: 20px;
  line-height: 20px;
  font-weight: bold;
  inset: 0 0 auto auto; /* top 和 right 的值 0 */
  transform-origin: 0 0; /* 或 top left */
  transform: translate(29.3%) rotate(45deg);
  background: #FFE14C;
  /*box-shadow: 0 0 0 999px #8FBCDA;*/
  box-shadow: 0 0 0 999px #FFE14C;
  clip-path: inset(0 -100%);
}


/* 角标 end */

/* 按钮组固定在右下角 */
.button-group {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: auto; /* 关键属性，使按钮组固定在底部 */
  /*padding-top: 10px;*/
}

/**
====== 一个方框 end ===
 */


</style>

